package org.tity.commons.environment;

import java.util.ArrayList;

 
/**
 * 性能时间戳探针
 * @author LeeFangJie
 */
public class TimeNeedle {
	private String name = null;
	public TimeNeedle(String name)
	{
		this.name=name;
	}
	public TimeNeedle() {
		this.name="noname";
	}
	
	private ArrayList<String> names=new ArrayList<String>();
	private ArrayList<Long> times=new ArrayList<Long>();
	
	public long begin()
	{
		long t=System.currentTimeMillis();
		times.add(t);
		names.add(this.name+"开始");
		return t;
	}
	
	public long stab()
	{
		return stab("p-"+names.size());
	}
	
	public long stab(String name)
	{
		long t=System.currentTimeMillis();
		times.add(t);
		names.add(name);
		return t;
	}
	
	public long end()
	{
		long t=System.currentTimeMillis();
		times.add(t);
		names.add(this.name+"结束");
		
		print();
		
		return t;
	}
	
	
	private void print() {
		
		long prevT=times.get(0);
		String prevN=names.get(0);
		long t;String n;
		StringBuilder buf=new StringBuilder();
		for (int i = 1; i < names.size(); i++) {
			t=times.get(i);
			n=names.get(i);
			buf.append(prevN+" 耗时 "+(t-prevT)+"ms\n");
			prevT=t;
			prevN=n;
		}
		System.out.println(buf);
		
	}
	
}
